﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
using Gmis.Public;
using System.Configuration;

namespace Gmis.Web.Manage
{
    /// <summary>
    /// UploadFile 的摘要说明
    /// 文件上传公用处理模块 1、村庄概况 上传图片的 action=village  目录结构 UploadFile/Village/20120202/llll.jpg
    /// 
    /// </summary>
    public class UploadFile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            switch (action.ToLower())
            {
                case "village":
                    VillageEditorUpload(context);
                    break;
                case "document":
                    DocumentUpload(context);
                    break;
                case "pmember":
                    PMemberUpload(context);
                    break;
                case "org":
                    OrganizationUpload(context);
                    break;

                case "system":
                    SystemUpload(context);
                    break;
            }
        }
        //村庄概况编辑器包含图片
        public void VillageEditorUpload(HttpContext context)
        {
            string err = string.Empty;
            HttpPostedFile file = null;
            string rootPath = "";
            string filePath = "";
            string oppositePath = "";

            try
            {
                file = context.Request.Files["filedata"]; //获取图片文件
                rootPath = context.Server.MapPath("~/");
                filePath = rootPath;
                filePath += "UpLoadFile";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
                filePath += "/Village";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
                filePath += "/" + DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "");
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
                filePath +="/"+ CommonFunction.GetGuid() + Path.GetExtension(file.FileName);
                file.SaveAs(filePath);//context.Server.MapPath("~/UpLoadFile/" + Path.GetFileName(file.FileName))
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }
            oppositePath = filePath.Replace(rootPath, "");
            JavaScriptSerializer s = new JavaScriptSerializer();
            context.Response.Write(s.Serialize(new
            {
                err = err,
                msg = err == string.Empty ? ConfigurationManager.AppSettings["URLHeader"].ToString() + oppositePath : string.Empty
            }));
        }

        //党员图片
        public void PMemberUpload(HttpContext context)
        {
            string err = string.Empty;
            HttpPostedFile file = null;
            string rootPath = "";
            string filePath = "";
            string oppositePath = "";

            try
            {
                file = context.Request.Files["filedata"]; //获取图片文件
                rootPath = context.Server.MapPath("~/");
                filePath = rootPath;

                filePath += "UpLoadFile";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/PMember";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/" + CommonFunction.GetGuid() + Path.GetExtension(file.FileName);
                file.SaveAs(filePath);//context.Server.MapPath("~/UpLoadFile/" + Path.GetFileName(file.FileName))
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }
            oppositePath = filePath.Replace(rootPath, "");
            JavaScriptSerializer s = new JavaScriptSerializer();
            context.Response.Write(s.Serialize(new
            {
                err = err,
                msg = err == string.Empty ? ConfigurationManager.AppSettings["URLHeader"].ToString() + oppositePath : string.Empty
            }));
        }

        //文档图片
        public void DocumentUpload(HttpContext context)
        {
            string err = string.Empty;
            HttpPostedFile file = null;
            string rootPath = "";
            string filePath = "";
            string oppositePath = "";

            try
            {
                file = context.Request.Files["filedata"]; //获取图片文件
                rootPath = context.Server.MapPath("~/");
                filePath = rootPath;

                filePath += "UpLoadFile";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/Document";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/" + CommonFunction.GetGuid() + Path.GetExtension(file.FileName);
                file.SaveAs(filePath);//context.Server.MapPath("~/UpLoadFile/" + Path.GetFileName(file.FileName))
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }
            oppositePath = filePath.Replace(rootPath, "");
            JavaScriptSerializer s = new JavaScriptSerializer();
            context.Response.Write(s.Serialize(new
            {
                err = err,
                msg = err == string.Empty ? ConfigurationManager.AppSettings["URLHeader"].ToString() + oppositePath : string.Empty
            }));
        }

        //组织成员图片
        public void OrganizationUpload(HttpContext context)
        {
            string err = string.Empty;
            HttpPostedFile file = null;
            string rootPath = "";
            string filePath = "";
            string oppositePath = "";

            try
            {
                file = context.Request.Files["filedata"]; //获取图片文件
                rootPath = context.Server.MapPath("~/");
                filePath = rootPath;

                filePath += "UpLoadFile";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/Organization";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/" + CommonFunction.GetGuid() + Path.GetExtension(file.FileName);
                file.SaveAs(filePath);
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }
            oppositePath = filePath.Replace(rootPath, "");
            JavaScriptSerializer s = new JavaScriptSerializer();
            context.Response.Write(s.Serialize(new
            {
                err = err,
                msg = err == string.Empty ? ConfigurationManager.AppSettings["URLHeader"].ToString() + oppositePath : string.Empty
            }));
        }


        //制度文件图片
        public void SystemUpload(HttpContext context)
        {
            string err = string.Empty;
            HttpPostedFile file = null;
            string rootPath = "";
            string filePath = "";
            string oppositePath = "";

            try
            {
                file = context.Request.Files["filedata"]; //获取图片文件
                rootPath = context.Server.MapPath("~/");
                filePath = rootPath;

                filePath += "UpLoadFile";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/System";
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);

                filePath += "/" + CommonFunction.GetGuid() + Path.GetExtension(file.FileName);
                file.SaveAs(filePath);
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }
            oppositePath = filePath.Replace(rootPath, "");
            JavaScriptSerializer s = new JavaScriptSerializer();
            context.Response.Write(s.Serialize(new
            {
                err = err,
                msg = err == string.Empty ? ConfigurationManager.AppSettings["URLHeader"].ToString() + oppositePath : string.Empty
            }));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}